package com.leo.ktvplayer.ui.setting;

import android.annotation.TargetApi;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.leo.ktvplayer.R;
import com.leo.ktvplayer.base.ActBase;
import com.leo.ktvplayer.domain.bean.Setting;
import com.leo.ktvplayer.domain.bean.User;
import com.leo.ktvplayer.localcookie.LocalUtils;
import com.leo.ktvplayer.util.ToastUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by LeoLu on 2017/2/27.
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class ActSetting extends ActBase {
    private Setting localSetting;
    private User localUser;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);


    }

    @Override
    protected void init() {
        super.init();
        localUser = LocalUtils.getUser();
        localSetting = LocalUtils.getSetting();
        ed_alipay_APPID.setText(localUser.getAlipay_appid());
        ed_alipay_PRIVATE_KEY.setText(localUser.getAlipay_key());
        ed_wechat_APPID.setText(localUser.getWechat_appid());
        ed_wechat_MchId.setText(localUser.getWechat_mchid());
        ed_wechat_PRIVATE_KEY.setText(localUser.getWechat_key());
        ed_time_1.setText(localSetting.getTime1() + "");
        ed_time_2.setText(localSetting.getTime2() + "");
        ed_time_3.setText(localSetting.getTime3() + "");
        ed_money_1.setText(localSetting.getPrice1() + "");
        ed_money_2.setText(localSetting.getPrice2() + "");
        ed_money_3.setText(localSetting.getPrice3() + "");
        ed_userPwd.setText(localUser.getUserpwd());



    }

    @OnClick(R.id.btn_save)
    public void save(View view){
        //TODO  save some user msg.
        String alipayAppid = ed_alipay_APPID.getText().toString();
        String alipayPrivatekey = ed_alipay_PRIVATE_KEY.getText().toString();
        String wechatAppid = ed_wechat_APPID.getText().toString();
        String wechatPrivatekey = ed_wechat_PRIVATE_KEY.getText().toString();
        String wechatMchid = ed_wechat_MchId.getText().toString();
        String pwd = ed_userPwd.getText().toString();
        String money1 = ed_money_1.getText().toString();
        String money2 = ed_money_2.getText().toString();
        String money3 = ed_money_3.getText().toString();
        String time1 = ed_time_1.getText().toString();
        String time2 = ed_time_2.getText().toString();
        String time3 = ed_time_3.getText().toString();

        Log.d("actSetting","-->" + pwd);
        if (pwd.contains(" ") || pwd.contains("\n")){
            ToastUtil.show("密码不能有空格");
            return;
        }

        if (TextUtils.isEmpty(alipayAppid) || TextUtils.isEmpty(alipayPrivatekey)
                || TextUtils.isEmpty(wechatAppid) || TextUtils.isEmpty(wechatPrivatekey)
                || TextUtils.isEmpty(wechatMchid) || TextUtils.isEmpty(pwd)
                || TextUtils.isEmpty(money1) || TextUtils.isEmpty(money3)
                || TextUtils.isEmpty(money2) ||TextUtils.isEmpty(time1) ||
                TextUtils.isEmpty(time2) ||TextUtils.isEmpty(time3)
                )
        {
            ToastUtil.show("重要信息不能为空");
            return;
        }

        localUser.setAlipay_appid(ed_alipay_APPID.getText().toString());
        localUser.setAlipay_key(ed_alipay_PRIVATE_KEY.getText().toString());
        localUser.setUserpwd(ed_userPwd.getText().toString());
        localUser.setWechat_appid(ed_wechat_APPID.getText().toString());
        localUser.setWechat_mchid(ed_wechat_MchId.getText().toString());
        localUser.setWechat_key(ed_wechat_PRIVATE_KEY.getText().toString());

        localSetting.setPrice1(Integer.parseInt(ed_money_1.getText().toString()));
        localSetting.setPrice2(Integer.parseInt(ed_money_2.getText().toString()));
        localSetting.setPrice3(Integer.parseInt(ed_money_3.getText().toString()));
        localSetting.setTime1(Integer.parseInt(ed_time_1.getText().toString()));
        localSetting.setTime2(Integer.parseInt(ed_time_2.getText().toString()));
        localSetting.setTime3(Integer.parseInt(ed_time_3.getText().toString()));

        LocalUtils.saveSetting(localSetting);
        LocalUtils.saveUser(localUser);

        finish();
    }

    @OnClick(R.id.btn_activity_setting_bleconnect)
    public void bleConnect(View view){
        BleScanResultDialog dialog = new BleScanResultDialog(this,R.style.dialog);
        dialog.show();
    }



    @BindView(R.id.ed_activity_setting_alipay_appid)
    public EditText ed_alipay_APPID;

    @BindView(R.id.ed_activity_setting_alipay_privatekey)
    public EditText ed_alipay_PRIVATE_KEY;

    @BindView(R.id.ed_activity_setting_wechat_mchid)
    public EditText ed_wechat_MchId;

    @BindView(R.id.ed_activity_setting_wechat_appid)
    public EditText ed_wechat_APPID;

    @BindView(R.id.ed_activity_setting_wechat_privatekey)
    public EditText ed_wechat_PRIVATE_KEY;

    @BindView(R.id.ed_activity_setting_play_time1)
    public EditText ed_time_1;

    @BindView(R.id.ed_activity_setting_play_time2)
    public EditText ed_time_2;

    @BindView(R.id.ed_activity_setting_play_time3)
    public EditText ed_time_3;

    @BindView(R.id.ed_activity_setting_money1)
    public EditText ed_money_1;

    @BindView(R.id.ed_activity_setting_money2)
    public EditText ed_money_2;

    @BindView(R.id.ed_activity_setting_money3)
    public EditText ed_money_3;

    @BindView(R.id.ed_activity_setting_adminpwd)
    public EditText ed_userPwd;

}
